package com.pacs.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import com.pacs.model._MappingKit;
import com.pacs.service.ServiceFactory;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.ext.directive.Str;

public class HtmlFilter {
	private static final Pattern IMG_P = Pattern.compile("<img .*?>");
	private static final Pattern IMG_SRC_P = Pattern.compile("src=\".*?\"");
	private static final Pattern HTML_STYPE_P = Pattern.compile("<style>.*?</style>",
			Pattern.DOTALL | Pattern.CASE_INSENSITIVE); // 过滤样式


	/**
	 * 过滤Html标签
	 * 
	 * @param html
	 * @return
	 */
	public static String filter(String html) {
		if (html == null || html.isEmpty()) {
			return "";
		}

		// 过滤掉样式
		Matcher m = HTML_STYPE_P.matcher(html);
		html = m.replaceAll("");

		StringBuilder str = new StringBuilder();
		int status = 1;
		for (int i = 0; i < html.length(); ++i) {
			char c = html.charAt(i);
			switch (c) {
			case '<':
				status = 0;
				break;
			case '>':
				status = 1;
				break;
			default:
				if (status == 1) {
					str.append(c);
				}
			}
		}
		String content = str.toString();
		return content.replaceAll("&.*?;", "");
	}

	/**
	 *  处理img标签，统一图片样式
	 * 
	 * @param html
	 * @return
	 */
	public static String processImg(String html) {
		Matcher imgM = IMG_P.matcher(html);
		StringBuilder content = new StringBuilder();
		int start = 0;
		while (imgM.find()) {
			String imgStr = imgM.group(0);
			Matcher srcM = IMG_SRC_P.matcher(imgStr);
			if (srcM.find()) {
				content.append(html.substring(start, imgM.start()));
				content.append("<img ").append(srcM.group(0)).append(" style=\"width: 100%;display: block\" />");
				start = imgM.end();
			}
		}
		if (start > 0) {
			return content.append(html.substring(start)).toString();
		} else {
			return html;
		}
	}

	public static List<String> findHtmlImages(String html) {
		List<String> imagesArr = new ArrayList<>();
		Matcher imgM = IMG_P.matcher(html);
		while (imgM.find()) {
			String imgStr = imgM.group(0);
			Matcher srcM = IMG_SRC_P.matcher(imgStr);
			if (srcM.find()) {
				String imgUrl = "<img " + srcM.group(0) + " />";
				imagesArr.add(imgUrl);
			}
		}
		return imagesArr;
	}

	/**
	 * 去掉样式
	 * 
	 * @param html
	 * @return
	 */
	public static String filterStyle(String html) {
		Matcher m = HTML_STYPE_P.matcher(html);
		return m.replaceAll("");
	}

	/**
	 * xss 过滤
	 * 
	 * @param html
	 * @return
	 */
	public static String filterXSS(String html) {
		if (html == null || html.isEmpty()) {
			return html;
		}
		return Jsoup.clean(html, Whitelist.basic());
	}

	/**
	 * xss 过滤
	 * 
	 * @param html
	 * @return
	 */
	public static String filterXssRelaxed(String html) {
		if (html == null || html.isEmpty()) {
			return html;
		}
		return Jsoup.clean(html, Whitelist.relaxed());
	}

	public static void main(String[] args) {
//		Articles articles = Articles.dao.findById(130);
//		List<String> ss = findHtmlImages(articles.getHtml());
//		System.out.println(ss);
	}
}
